﻿using NewRen.Helpers;
using NewRen.Models;
using NewRen.Models.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Controllers
{
    public class CommonController : Controller
    {
        public DB db = new DB();
        // GET: Common
        public ActionResult Index()
        {
            return View();
        }

        #region 得到城市
        /// <summary>
        /// 得到城市
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCities()
        {
            AjaxModel ajaxModel = new AjaxModel();
            var cities = db.Cities.ToList();
            ajaxModel.Data = cities;
            ajaxModel.Statu = "ok";
            ajaxModel.Msg = "获取成功";
            return Json(ajaxModel, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region 根据城市获取区县
        /// <summary>
        /// 根据城市获取区县 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetDistrictsByCID(int id)
        {
            AjaxModel ajaxModel = new AjaxModel();
            try
            {
                var districts = db.Districts.Where(d => d.CityID == id).ToList();
                ajaxModel.Data = districts;
                ajaxModel.Statu = "ok";
                ajaxModel.Msg = "获取成功";
            }
            catch
            {
                ajaxModel.Statu = "err";
                ajaxModel.Msg = "获取失败！";
            }
            return Json(ajaxModel, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region 根据区县得到镇
        /// <summary>
        /// 根据区县得到镇
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetTownsByDID(int id)
        {
            AjaxModel ajaxModel = new AjaxModel();
            try
            {
                var towns = db.Towns.Where(t => t.DistrictID == id).ToList();
                ajaxModel.Statu = "ok";
                ajaxModel.Msg = "获取成功";
                ajaxModel.Data = towns;
            }
            catch
            {
                ajaxModel.Statu = "err";
                ajaxModel.Msg = "获取失败";
            }

            return Json(ajaxModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 获取行业
        /// <summary>
        /// 获取行业
        /// </summary>
        /// <returns></returns>
        public ActionResult GetIndustries()
        {
            AjaxModel ajaxModel = new AjaxModel();
            var industries = db.Industries.ToList();
            ajaxModel.Statu = "ok";
            ajaxModel.Msg = "获取成功";
            ajaxModel.Data = industries;
            return Json(ajaxModel, JsonRequestBehavior.AllowGet);
        }


        #endregion



       

    }
}